package com.ops.wechat.dao;

import com.ops.uitl.data.IData;
import com.ops.uitl.data.IDataset;
import com.ops.uitl.tapestry.AppEntity;
import com.ops.uitl.tapestry.PageData;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * lenovo
 * 2017/5/10
 * untitled
 */
public class CommonDAO extends AppEntity {
	public CommonDAO(PageData pd) throws Exception {
		super(pd, "sdcen1");
	}

	/**
	 * @Describe 获取序列
	 * @Method getSeq
	 * @Param
	 * @Author liyl@asiainfo.com
	 * @Date 2017/5/10 21:10
	 */
	public String getSeq(String seq) throws Exception {
		String sql = "select F_ZYZX_GETSEQID('"+seq+"') seq_id from dual";
		IDataset dataset = queryList(sql);
		String seq_id = null;
		if (null != dataset && dataset.size()>0) {
			IData data = (IData) dataset.get(0);
			seq_id = data.getString("SEQ_ID");
		}
		return seq_id;
	}


	public String getSys_date() throws Exception {
		String sql = "select to_char(SYSDATE,'yyyy-MM-dd HH24:mi:ss') sys_time from dual";
		IDataset dataset = queryList(sql);
		String seq_id = "2000-01-01 00:00:00";
		if (null != dataset && dataset.size()>0) {
			IData data = (IData) dataset.get(0);
			seq_id = data.getString("SYS_TIME");
		}
		return  seq_id;
	}

	/**
	 * 查询活动数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public IDataset getActivityInfo(IData param) throws Exception{
		return  this.queryListByCodeCodeParser("TF_ZYZX_ACTIVITY_DETAIL", "GET_ACTIVITY_DETAIL", param);
	}

	/**
	 * 插入活动数据
	 * @param pd
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public boolean insertActivity(PageData pd, IData param)
			throws Exception {

		Connection con = pd.getDBConn("sdcen1");
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		int state = 0;
		sql.append(" INSERT INTO TF_ZYZX_ACTIVITY_DETAIL ");
		sql.append(" (RE_ID,SERIAL_NUMBER,OTHER_SERIAL_NUMBER,OPEN_ID,BRAND_CODE,EPARCHY_CODE,CHAN_ID,SALE_TYPE," +
				"SALE_NAME,REWARD_ID,REWARD_NAME,REWARD_TYPE,BUSI_PARAM1,BUSI_PARAM2,BUSI_PARAM3,BUSI_PARAM4,REWARD_STATE," +
				"REWARD_FLAG,IN_TIME,OPER_TIME,UPDATE_TIME,REMARK,RSRV_STR1,RSRV_STR2,RSRV_STR3) values");
		sql.append(" (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate,?,?,?,?,?,?)");
		PreparedStatement ps = con.prepareStatement(sql.toString());
		try {
			ps.setString(1, param.getString("RE_ID"));
			ps.setString(2, param.getString("SERIAL_NUMBER"));
			ps.setString(3, param.getString("OTHER_SERIAL_NUMBER"));
			ps.setString(4, param.getString("OPEN_ID"));
			ps.setString(5, param.getString("BRAND_CODE"));
			ps.setString(6, param.getString("EPARCHY_CODE"));
			ps.setString(7, param.getString("CHAN_ID"));
			ps.setString(8, param.getString("SALE_TYPE"));
			ps.setString(9, param.getString("SALE_NAME"));
			ps.setString(10, param.getString("REWARD_ID"));
			ps.setString(11, param.getString("REWARD_NAME"));
			ps.setString(12, param.getString("REWARD_TYPE"));
			ps.setString(13, param.getString("BUSI_PARAM1"));
			ps.setString(14, param.getString("BUSI_PARAM2"));
			ps.setString(15, param.getString("BUSI_PARAM3"));
			ps.setString(16, param.getString("BUSI_PARAM4"));
			ps.setString(17, param.getString("REWARD_STATE"));
			ps.setString(18, param.getString("REWARD_FLAG"));
			ps.setString(19, param.getString("OPER_TIME"));
			ps.setString(20, param.getString("UPDATE_TIME"));
			ps.setString(21, param.getString("REMARK"));
			ps.setString(22, param.getString("RSRV_STR1"));
			ps.setString(23, param.getString("RSRV_STR2"));
			ps.setString(24, param.getString("RSRV_STR3"));
			state = ps.executeUpdate();
			if (1 == state) {
				flag = true;
			}
			ps.close();
			con.commit();
		} catch (Exception e) {
			log.error(e, e);
		}
		return flag;
	}

	/**
	 * 插入用户绑定信息
	 * @param pd
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public boolean insertUserinfo(PageData pd, IData param, String operate)
			throws Exception {
		Connection con = pd.getDBConn("sdcen1");
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		int state = 0;
		try {
			PreparedStatement ps = null;
			if("insert".equals(operate)){
				sql.append(" INSERT INTO tf_zyzx_wxbind_user ");
				sql.append(" (serial_number,open_id,bind_time,last_bind_time,remark,RSRV_STR1,RSRV_STR2,RSRV_STR3) values");
				sql.append(" (?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),sysdate,?,?,?,?)");
				ps = con.prepareStatement(sql.toString());
				ps.setString(1, param.getString("SERIAL_NUMBER"));
				ps.setString(2, param.getString("OPEN_ID"));
				ps.setString(3, param.getString("BIND_TIME"));
				ps.setString(4, param.getString("REMARK"));
				ps.setString(5, param.getString("RSRV_STR1"));
				ps.setString(6, param.getString("RSRV_STR2"));
				ps.setString(7, param.getString("RSRV_STR3"));
			}else if("update".equals(operate)){
				sql.append(" UPDATE tf_zyzx_wxbind_user set last_bind_time=SYSDATE where ");
				sql.append(" serial_number=? AND OPEN_ID=?");
				ps = con.prepareStatement(sql.toString());
				ps.setString(1, param.getString("SERIAL_NUMBER"));
				ps.setString(2, param.getString("OPEN_ID"));
			}
			log.error("SQL:::::"+sql);

			state = ps.executeUpdate();
			if (1 == state) {
				flag = true;
			}
			ps.close();
			con.commit();
		} catch (Exception e) {
			log.error(e, e);
		}
		return flag;
	}

}
